package DataModel;

import java.io.File;
import java.util.ArrayList;

import Interfaces.Track;
import SortSearch.BinarySearch;

/**
 * This class represents a list where {@link Feature}s are stored in.
 * 
 * @author Stefan Lorenz <br>
 *         <b>Date:</b> 12.11.2012
 */
@SuppressWarnings("serial")
public class FeatureHolder extends ArrayList<Feature> implements Track {

	/**
	 * Parsed file
	 */
	private File file;
	/**
	 * Type of file e.g. 'bed','gff' etc.. Depends on the file format
	 */
	private String typeOfFile;

	/**
	 * Constructor of an empty {@link FeatureHolder}
	 */
	public FeatureHolder() {
		this.file = new File("");
		this.typeOfFile = "";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Interfaces.Track#getFile()
	 */
	public File getFile() {
		return this.file;
	}

	/**
	 * Sets the file where the data is parsed from
	 * 
	 * @param file
	 *            File of the parsed data
	 */
	public void setFile(File file) {
		this.file = file;
	}

	/**
	 * Returns the index of the {@link Feature} in this {@link FeatureHolder}
	 * given a region
	 * 
	 * @param start
	 *            Most left position
	 * @param stop
	 *            Most right position
	 * @return Index of the {@link Feature} where it is stored in this
	 *         {@link FeatureHolder}
	 */
	public int getFeatureIndexInRange(int start, int stop) {
		BinarySearch search = new BinarySearch();
		int featureIndex = search.binarySearchRead(this, start);

		if (featureIndex <= 100) {
			featureIndex = 0;
		} else {
			featureIndex -= 100;
		}
		return featureIndex;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Interfaces.Track#getTypeOfFile()
	 */
	public String getTypeOfFile() {
		return this.typeOfFile;
	}

	/**
	 * Returns the type of the file
	 * 
	 * @param type
	 *            Type of the file
	 */
	public void setTypeOfFile(String type) {
		this.typeOfFile = type;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Interfaces.Track#getName()
	 */
	public String getName() {
		return this.file.getName();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Interfaces.Track#getElementsOfTrack()
	 */
	public int getElementsOfTrack() {
		return this.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Interfaces.Track#getSize()
	 */
	public int getSize() {
		return this.get(this.size() - 1).getStop();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see Interfaces.Track#setTrackIndexToFeatures(int)
	 */
	@Override
	public void setTrackIndexToFeatures(int index) {
		for (int i = 0; i < this.size(); i++) {
			this.get(i).setTrackIndex(index);
		}
	}

	/**
	 * Returns the index of the Feature one is looking for
	 * 
	 * @param name
	 *            Name of the Feature one is looking for
	 * @return Index of the feeature
	 */
	public int getFeatureIndexByName(String name) {
		for (int i = 0; i < this.size(); i++) {
			if (this.get(i).getName().equals(name))
				return i;
		}
		return -1;
	}
}
